import './publicpath.js'
import { createApp } from 'vue'
import App from './App.vue'
import routes from './router'
import { createRouter, createWebHistory } from "vue-router";
import actions from './actions'

let instance;

function render(props = {}) {
  // 加载vue实例
  const { container, baseUrl } = props
  actions.setActions(props) // props中有onGlobalStateChange、setGlobalState等函数
  const router = createRouter({
    routes,
    base: window.__POWERED_BY_QIANKUN__ ? baseUrl : process.env.BASE_URL,
    history: createWebHistory(baseUrl)
  });
  instance = createApp(App)
  instance.use(router).mount(container ? container.querySelector('#sub-vue2') : '#sub-vue2')
}

// 独立运行微应用
if (!window.__POWERED_BY_QIANKUN__) {
  render();
}

// 抛出三个生命周期钩子，供主应用调用

export async function bootstrap() {}

export async function mount(props) {
  render(props)
}

export async function unmount() {
  instance?.unmount();
  instance = null;
}
